﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Common
{
    public class FlowHelper
    {
        // 是否有流程数据
        public static bool HasFlowData(Guid flowInstanceID)
        {
            try
            {
                DataTable dataTable = FlowHelper.GetFlowInstanceInfo(flowInstanceID);
                if (dataTable.Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 获取 FlowInstance 信息
        public static DataTable GetFlowInstanceInfo(Guid flowInstanceID)
        {
            string sql = @"SELECT * FROM dbo.FlowInstance WHERE FlowInstanceID = @FlowInstanceID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.Guid, flowInstanceID);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 获取流程定义数据
        public static DataTable GetFlowMain()
        {
            string sql = @"SELECT * FROM dbo.FlowMain";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 流程的当前步骤是第一步
        public static bool CurrentFlowStepIsFirstSetp(string flowID, string flowInstanceID)
        {
            string sql = @"SELECT COUNT(*) FROM dbo.FlowInstanceStep WHERE FlowInstanceID = @FlowInstanceID
                            AND Completed = 0
                            AND StepID IN (
	                            SELECT A.StepID FROM dbo.FlowStepRelation A
		                            INNER JOIN dbo.FlowStep B ON A.StepID = B.StepID AND B.FlowID = @FlowID
	                            WHERE A.ParentID IS NULL)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowID", DbType.String, flowID);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
                object obj = database.ExecuteScalar(dbCommand);
                return obj.ToString().Equals("0") ? false : true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 根据流程ID和步骤ID获取处理人的email
        public static string GetOperatorEmail(string flowID, string stepID)
        {
            string sql = @"SELECT B.Email FROM dbo.FlowStepOperator A
                          INNER JOIN dbo.t5715_User B ON A.OperatorInfo = B.Username
                          INNER JOIN dbo.FlowStep C ON A.StepID = C.StepID
                          WHERE A.StepID = @StepID AND C.FlowID = @FlowID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowID", DbType.String, flowID);
                database.AddInParameter(dbCommand, "@StepID", DbType.String, stepID);
                object obj = database.ExecuteScalar(dbCommand);
                return obj == null ? string.Empty : obj.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}